package com.awspaas.user.apps.shhtaerospaceindustrial.extbutton_zhpj;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ValueListener;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOAPI;
import com.actionsoft.sdk.local.api.ProcessAPI;
import java.util.List;
import java.util.Map;

public class createUnitJZhpjbyYbBillProsses extends ValueListener {
	@Override
	public String execute(ProcessExecutionContext param) throws Exception {
		// TODO Auto-generated method stub
		ResponseObject respon = ResponseObject.newOkResponse();
		String bindid = param.getProcessInstance().getId();

		String queryDdy = "SELECT YEARINFO,RESUME,STARTDATE,ENDDATE FROM BO_EU_ZHPJ_YBMANAGE_HEAD WHERE BINDID = '"
				+ bindid + "'";
		String YEARINFO = objToStr(DBSql.getString(queryDdy, "YEARINFO"));
		String RESUME = objToStr(DBSql.getString(queryDdy, "RESUME"));
		String STARTDATE = objToStr(DBSql.getString(queryDdy, "STARTDATE"));
		String ENDDATE = objToStr(DBSql.getString(queryDdy, "ENDDATE"));

//	        System.out.println("bindid::" + bindid);
		String querySql = "select ID,APPLYUNIT,APPLYUSERNAME,YBDEPTAUDITSTATUS,YBDEPTAUDITPROSID from "
				+ "BO_EU_ZHPJ_YBMANAGE_PSNMX where " + "bindid='" + bindid + "'";
		List<Map<String, Object>> dataList = DBSql.query(querySql, new ColumnMapRowMapper());
		if (dataList != null && dataList.isEmpty()) {

			List<Map<String, Object>> psndataList = getLaoZonglist();

			for (Map<String, Object> dataMap : psndataList) {
//						JSONObject orderItem = new JSONObject();
				String ID = objToStr(dataMap.get("ID"));
				String APPLYUNIT = objToStr(dataMap.get("APPLYUNIT"));
				String APPLYDEPTNAME = objToStr(dataMap.get("APPLYDEPTNAME"));
				String APPLYUSERNAME = objToStr(dataMap.get("APPLYUSERNAME"));
				String APPLYUSERNZH = objToStr(dataMap.get("APPLYUSERNZH"));

				if (APPLYUSERNZH != null && !("").equals(APPLYUSERNZH)) {

					String prosuuid = "obj_492d2f3ad33944f8a33b6cbb336a60e6"; // 综合评价流程ID
					ProcessAPI proapi = SDK.getProcessAPI();
					// 创建了一个流程实例，第一个参数为流程UUID
					ProcessInstance processInstance = proapi.createProcessInstance(prosuuid, APPLYUSERNZH,
							YEARINFO + "年" + "院部各部门综合评价");
					processInstance.getEndActivityId();

					System.out.println("流程实例对象是：" + processInstance + "\n流程实例ID:" + processInstance.getId());
					// 获取流程状态
					String proState = processInstance.getControlState();
					System.out.println("当前流程状态：" + proState);
					// 启动了这个流程实例
					proapi.start(processInstance, "");
					// 获得流程的taskId，启动后才有任务实例的存在
//		                    String taskid1 = processInstance.getStartTaskInstId();
//		                    System.out.println("taskID1::" + taskid1);

					// 准备数据
					BOAPI boAPI = SDK.getBOAPI();
					String boname = "BO_EU_ZHPJ_PSN_AUDIT_HAED"; // 两总评价主表
					BO boRecordData = new BO();
					boRecordData.set("APPLYUNIT", APPLYUNIT);
					boRecordData.set("APPLYDEPTNAME", APPLYDEPTNAME);
					boRecordData.set("APPLYUSERNAME", APPLYUSERNAME);
					boRecordData.set("APPLYUSERZH", APPLYUSERNZH);
					boRecordData.set("AUDITTYPE", "院部各部门");

					boRecordData.set("YEARINFO", YEARINFO);
					boRecordData.set("BDATE", STARTDATE);
					boRecordData.set("EDATE", ENDDATE);
					boRecordData.set("NOTE", RESUME);

					boRecordData.set("SOURCEPORJECTID", bindid);// 记录来源id为院部发起的流程实例ID

					int actionflag = boAPI.create(boname, boRecordData, processInstance, param.getUserContext());

//					String queryybdeptSql = "select ID,UNITNAME from "
//							+ " BO_EU_ZHPJ_XXWH_UNITLIST where     UNITTYPE='院部部门'";
//					List<Map<String, Object>> deptdataList = DBSql.query(queryybdeptSql, new ColumnMapRowMapper());
//
//					if (deptdataList != null && !deptdataList.isEmpty()) {
//						List<BO> items = new ArrayList<BO>();
//						String itemboname = "BO_EU_ZHPJ_PSN_AUDIT_MX";
//						for (Map<String, Object> itemdataMap : deptdataList) {
//							String UNITNAME = objToStr(itemdataMap.get("UNITNAME"));
//
//							BO itemRecordData = new BO();
//							itemRecordData.set("UNITNAME", UNITNAME);
//
//							items.add(itemRecordData);
//						}
//						if (items.size() > 0) {
//							int[] itemsnum = boAPI.create(itemboname, items, processInstance, param.getUserContext());
//						}
//					}

					String ybglzbboname = "BO_EU_ZHPJ_YBMANAGE_PSNMX"; // 两总评价主表
					BO yzzbboRecordData = new BO();
					yzzbboRecordData.set("APPLYUNIT", APPLYUNIT);
					yzzbboRecordData.set("APPLYDEPTNAME", APPLYDEPTNAME);
					yzzbboRecordData.set("APPLYUSERNAME", APPLYUSERNAME);
					yzzbboRecordData.set("APPLYUSERNZH", APPLYUSERNZH);

					yzzbboRecordData.set("YBDEPTAUDITSTATUS", "未完成");
					yzzbboRecordData.set("YBDEPTAUDITPROSID", processInstance.getId());
					int actionflag2 = boAPI.create(ybglzbboname, yzzbboRecordData, param.getProcessInstance(),
							param.getUserContext());

				}

			}

		} else
		{

			String querysqlfornotjoinpsn = "select ID,APPLYUNIT,APPLYDEPTNAME,APPLYUSERNAME,APPLYUSERNZH from BO_EU_ZHPJ_XXWH_PSNLIST t "
					+ " where t.applyusernzh not in (select applyusernzh from BO_EU_ZHPJ_YBMANAGE_PSNMX  where bindid='"
					+ bindid + "')";
			List<Map<String, Object>> notinpsnList = DBSql.query(querysqlfornotjoinpsn, new ColumnMapRowMapper());
			if (notinpsnList != null && !notinpsnList.isEmpty()) {
				
				for (Map<String, Object> dataMap : notinpsnList) {
					String ID = objToStr(dataMap.get("ID"));
					String APPLYUNIT = objToStr(dataMap.get("APPLYUNIT"));
					String APPLYDEPTNAME = objToStr(dataMap.get("APPLYDEPTNAME"));
					String APPLYUSERNAME = objToStr(dataMap.get("APPLYUSERNAME"));
					String APPLYUSERNZH = objToStr(dataMap.get("APPLYUSERNZH"));

					if (APPLYUSERNZH != null && !("").equals(APPLYUSERNZH)) {

						String prosuuid = "obj_492d2f3ad33944f8a33b6cbb336a60e6"; // 综合评价流程ID
						ProcessAPI proapi = SDK.getProcessAPI();
						// 创建了一个流程实例，第一个参数为流程UUID
						ProcessInstance processInstance = proapi.createProcessInstance(prosuuid, APPLYUSERNZH,
								YEARINFO + "年" + "院部各部门综合评价");
						processInstance.getEndActivityId();

						System.out.println("流程实例对象是：" + processInstance + "\n流程实例ID:" + processInstance.getId());
						// 获取流程状态
						String proState = processInstance.getControlState();
						System.out.println("当前流程状态：" + proState);
						// 启动了这个流程实例
						proapi.start(processInstance, "");
						// 获得流程的taskId，启动后才有任务实例的存在
//			                    String taskid1 = processInstance.getStartTaskInstId();
//			                    System.out.println("taskID1::" + taskid1);

						// 准备数据
						BOAPI boAPI = SDK.getBOAPI();
						String boname = "BO_EU_ZHPJ_PSN_AUDIT_HAED"; // 两总评价主表
						BO boRecordData = new BO();
						boRecordData.set("APPLYUNIT", APPLYUNIT);
						boRecordData.set("APPLYDEPTNAME", APPLYDEPTNAME);
						boRecordData.set("APPLYUSERNAME", APPLYUSERNAME);
						boRecordData.set("APPLYUSERZH", APPLYUSERNZH);
						boRecordData.set("AUDITTYPE", "院部各部门");

						boRecordData.set("YEARINFO", YEARINFO);
						boRecordData.set("BDATE", STARTDATE);
						boRecordData.set("EDATE", ENDDATE);
						boRecordData.set("NOTE", RESUME);

						boRecordData.set("SOURCEPORJECTID", bindid);// 记录来源id为院部发起的流程实例ID

						int actionflag = boAPI.create(boname, boRecordData, processInstance, param.getUserContext());

//						String queryybdeptSql = "select ID,UNITNAME from "
//								+ " BO_EU_ZHPJ_XXWH_UNITLIST where     UNITTYPE='院部部门'";
//						List<Map<String, Object>> deptdataList = DBSql.query(queryybdeptSql, new ColumnMapRowMapper());
//
//						if (deptdataList != null && !deptdataList.isEmpty()) {
//							List<BO> items = new ArrayList<BO>();
//							String itemboname = "BO_EU_ZHPJ_PSN_AUDIT_MX";
//							for (Map<String, Object> itemdataMap : deptdataList) {
//								String UNITNAME = objToStr(itemdataMap.get("UNITNAME"));
//
//								BO itemRecordData = new BO();
//								itemRecordData.set("UNITNAME", UNITNAME);
//
//								items.add(itemRecordData);
//							}
//							if (items.size() > 0) {
//								int[] itemsnum = boAPI.create(itemboname, items, processInstance,
//										param.getUserContext());
//							}
//						}

						String ybglzbboname = "BO_EU_ZHPJ_YBMANAGE_PSNMX"; // 两总评价主表
						BO yzzbboRecordData = new BO();
						yzzbboRecordData.set("APPLYUNIT", APPLYUNIT);
						yzzbboRecordData.set("APPLYDEPTNAME", APPLYDEPTNAME);
						yzzbboRecordData.set("APPLYUSERNAME", APPLYUSERNAME);
						yzzbboRecordData.set("APPLYUSERNZH", APPLYUSERNZH);

						yzzbboRecordData.set("YBDEPTAUDITSTATUS", "未完成");
						yzzbboRecordData.set("YBDEPTAUDITPROSID", processInstance.getId());
						int actionflag2 = boAPI.create(ybglzbboname, yzzbboRecordData, param.getProcessInstance(),
								param.getUserContext());

					}

				}
			
			
		}

//			respon.put("action", "success");
		}
		return "操作成功";
	}

	public static String objToStr(Object obj) {
		return obj == null ? "" : obj.toString();
	}

	protected List getLaoZonglist() {

		String querypsnSql = "select ID,APPLYUNIT,APPLYDEPTNAME,APPLYUSERNAME,APPLYUSERNZH from "
				+ " BO_EU_ZHPJ_XXWH_PSNLIST where     APPLYUSERNZH is  not null";
		List<Map<String, Object>> psndataList = DBSql.query(querypsnSql, new ColumnMapRowMapper());

		if (psndataList != null && !psndataList.isEmpty()) {
			return psndataList;
		}
		return null;
	}

}
